package com.lcq.study.leetcode;

/**
 * @ClassName T20220406
 * @Description
 * @Author lichuanqi
 * @Date 2022/4/6 10:31
 * @Version 1.0
 **/
public class T20220406 {
    public static void main(String[] args) {
        T20220406 test = new T20220406();
        int[] nums = {-1,-100,3,99};
        test.rotate1(nums, 2);
        for (int num : nums) {
            System.out.print(num+" ");
        }
    }

    public void rotate1(int[] nums, int k) {
        int length = nums.length;
        int modulo = k%length;
        //保证全部元素都访问过
        boolean[] visit = new boolean[length];
        if (modulo == 0){
            return;
        }
        for (int i = 0; i < length; i++) {
            if (visit[i] != true){
                swap(nums, i, modulo, visit);
            }
        }
    }

    public void swap(int nums[], int index, int modulo, boolean[] visit){
        int length = nums.length;
        int targetIndex = (index+modulo)%length;
        if (visit[targetIndex] == true){
            return;
        }
        visit[index] = true;
        swap(nums, targetIndex, modulo, visit);
        int temp = nums[index];
        nums[index] = nums[targetIndex];
        nums[targetIndex] = temp;
    }

    public void rotate(int[] nums, int k) {
        int length = nums.length;
        int modulo = k%length;
        if (modulo == 0){
            return;
        }
        int [] nums1 = new int[length];

        for (int i = 0; i < length; i++) {
            nums1[(i+modulo)%length] = nums[i];
        }

        for (int i = 0; i < length; i++) {
            nums[i] = nums1[i];
        }

    }




}
